﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace SilverlightDragDrop
{
  public class DragSource : ContentControl
  {
    public ControlTemplate GhostTemplate { get; set; }

    private GroupCollection groups = new GroupCollection();
    public GroupCollection Groups { get { return groups; } set { groups = value; } }

    public DragSource()
    {
      this.MouseLeftButtonDown += new MouseButtonEventHandler(DragSource_MouseLeftButtonDown);
      this.MouseMove += new MouseEventHandler(DragSource_MouseMove);
    }

    private void DragSource_MouseMove(object sender, MouseEventArgs e)
    {
      if (!DragDrop.IsDragging) return;

      DragDrop.DragMove(e.GetPosition(null));
    }

    private void DragSource_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
      this.CaptureMouse();
      DragDrop.StartDrag(this, GhostTemplate);
    }

    protected override void OnLostMouseCapture(MouseEventArgs e)
    {
      base.OnLostMouseCapture(e);
      DragDrop.DragEnded(e.GetPosition(null));
    }

  }
}
